package com.xhcl.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xhcl.system.mapper.TGgYskjkmgjMapper;
import com.xhcl.system.domain.HsGjFsDw;
import com.xhcl.system.domain.TGgPtdwgj;
import com.xhcl.system.domain.TGgYskjkmgj;
import com.xhcl.system.domain.YskmGjkmdm;
import com.xhcl.system.service.ITGgYskjkmgjService;
import com.xhcl.common.core.text.Convert;
import com.xhcl.common.exception.BusinessException;
import com.xhcl.common.utils.StringUtils;

/**
 * 预算科目会计科目挂接Service业务层处理
 * 
 * @author xhcl
 * @date 2020-08-29
 */
@Service
public class TGgYskjkmgjServiceImpl implements ITGgYskjkmgjService 
{
    @Autowired
    private TGgYskjkmgjMapper tGgYskjkmgjMapper;

    /**
     * 查询预算科目会计科目挂接
     * 
     * @param nd 预算科目会计科目挂接ID
     * @return 预算科目会计科目挂接
     */
    @Override
    public TGgYskjkmgj selectTGgYskjkmgjById(String yskm)
    {
        return tGgYskjkmgjMapper.selectTGgYskjkmgjById(yskm);
    }

    /**
     * 查询预算科目会计科目挂接列表
     * 
     * @param tGgYskjkmgj 预算科目会计科目挂接
     * @return 预算科目会计科目挂接
     */
    @Override
    public List<TGgYskjkmgj> selectTGgYskjkmgjList(TGgYskjkmgj tGgYskjkmgj)
    {
        return tGgYskjkmgjMapper.selectTGgYskjkmgjList(tGgYskjkmgj);
    }

    /**
     * 新增预算科目会计科目挂接
     * 
     * @param tGgYskjkmgj 预算科目会计科目挂接
     * @return 结果
     */
    @Override
    public int insertTGgYskjkmgj(TGgYskjkmgj tGgYskjkmgj)
    {
        return tGgYskjkmgjMapper.insertTGgYskjkmgj(tGgYskjkmgj);
    }

    /**
     * 修改预算科目会计科目挂接
     * 
     * @param tGgYskjkmgj 预算科目会计科目挂接
     * @return 结果
     */
    @Override
    public int updateTGgYskjkmgj(TGgYskjkmgj tGgYskjkmgj)
    {
        return tGgYskjkmgjMapper.updateTGgYskjkmgj(tGgYskjkmgj);
    }

    /**
     * 删除预算科目会计科目挂接对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTGgYskjkmgjByIds(String ids)
    {
        return tGgYskjkmgjMapper.deleteTGgYskjkmgjByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除预算科目会计科目挂接信息
     * 
     * @param nd 预算科目会计科目挂接ID
     * @return 结果
     */
    @Override
    public int deleteTGgYskjkmgjById(String yskm)
    {
        return tGgYskjkmgjMapper.deleteTGgYskjkmgjById(yskm);
    }
    
    @Transactional
    public int updateYskmGjkmdm(YskmGjkmdm yskmGjkmdm)
    {   
    	if (StringUtils.isNull(yskmGjkmdm))
	    {
	        throw new BusinessException("保存的数据为空");
	    }
    	
    	if (StringUtils.isNull(yskmGjkmdm.getYskm()))
	    {
	        throw new BusinessException("保存的预算科目为空");
	    }
    	if (StringUtils.isNull(yskmGjkmdm.getKmdms())||yskmGjkmdm.getKmdms().length==0)
	    {
	        throw new BusinessException("保存的科目代码为空");
	    }
    	tGgYskjkmgjMapper.deleteTGgYskjkmgjById(yskmGjkmdm.getYskm());
    	for (int i=0;i<yskmGjkmdm.getKmdms().length;i++){
    		TGgYskjkmgj tGgYskjkmgj=new TGgYskjkmgj();
    		tGgYskjkmgj.setYskm(yskmGjkmdm.getYskm());
    		tGgYskjkmgj.setKmdm(yskmGjkmdm.getKmdms()[i]);
    		tGgYskjkmgjMapper.insertTGgYskjkmgj(tGgYskjkmgj);
    	}
    	return yskmGjkmdm.getKmdms().length;
    }
}
